<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微圈文章添加</title>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <style>
        .layui-input{
            width: 450px;
        }
        .list{
            width: 1650px;
            height: 50px;
            margin-left: 20px;
            background-color:rgb(243,243,243);
        }
        .list-left{
            width: 100px;
            height: 30px;
            margin-top: 10px;
            float: left;
        }
        .list-left-left{
            width: 10px;
            height: 30px;
            float: left;
            background: green;
        }
        .list-left-right{
            width: 80px;
            height: 30px;
            font-size: 14px;
            line-height: 30px;
            float: left;
            text-indent: 5px;
        }
        .list-right{
            float: right;
            margin-top: 5px;
            margin-right: 45px;
        }
    </style>
</head>
<body>
<div class="list">
    <div class="list-left">
        <div class="list-left-left"></div>
        <p class="list-left-right">添加微圈</p>
    </div>
    <div class="list-right">
        <div class="list-right-left layui-btn layui-icon layui-icon-refresh" id="refresh">&nbsp;刷新</div>
        <div class="list-right-right layui-btn layui-icon layui-icon-left" id="goBack">&nbsp;返回</div>
    </div>
</div>
 <div style="width: 900px; height: 700px;  margin-left: 500px; margin-top: 50px;">

     <div class="layui-form"  lay-filter="layForm" style="width: 700px;">
         <div class="layui-form-item layui-row">
             <label class="layui-form-label">文章标题：</label>
             <div class="layui-input-inline">
                 <input class="layui-input" type="text" placeholder="文章标题" id="artcleTitle" name="artcleTitle"/>
                 <input class="layui-input" type="hidden" placeholder="微圈Id" id="microcircleId" name="microcircleId"/>
             </div>
         </div>

         <div class="layui-form-item layui-row">
             <label class="layui-form-label">内容类型：</label>
             <div class="layui-input-inline">
                 <input class="layui-input" type="text" placeholder="内容类型" id="artcleCategroyName" name="artcleCategroyName" disabled/>
                 <input class="layui-input" type="hidden" placeholder="" id="acticleCategroyId" name="acticleCategroyId"/>
             </div>
             <div>                     <!--选择-->
                 <i class="layui-icon layui-icon-layer" style="font-size: 30px; float: right; padding-right: 94px;" onclick="ArticleClassification()"></i>

             </div>

         </div>

         <div class="layui-form-item layui-row">
             <label class="layui-form-label">文章内容：</label>
             <textarea lay-verify="area" name="area" id="area" style="width: 675px; height: 500px; margin-left: 23px;"></textarea>
         </div>
          <div  style="float: right;">
                 <input type="button" class="layui-btn" onclick="draft()" value="保存草稿" />
                 <input type="button" class="layui-btn" onclick="release()" value="发布文章"/>
             </div>
     </div>
     <input type="hidden" id="state" value="2"/>
     <!-- 返回 -->
     <p id="title" style="display: none;">添加微圈</p>
 </div>
</body>
    <script>
        var layedit;
        layui.use(["layer","jquery","form","upload","layedit"],function () {
            var
                layer = layui.layer,
                $ = layui.jquery,
                form = layui.form,
                upload = layui.upload;
            layedit = layui.layedit;

            var user = JSON.parse(localStorage.getItem("user"));
            getJsMicrocircle();
            layedit.set({	//设置图片接口
                uploadImage: {
                    url: '/Upload/layeditUpload', //接口url
                    type: 'post'
                }
            });
            var area = layedit.build('area',{
                height: 300
            });
            //刷新
            $("#Refresh").click(function () {
                location.reload();
            });

            //返回
            $('#goBack').click(function () {
                var title = $('#title').text();
                var iframe = parent.$('.iframe');
                $.each(iframe,function (i,item) {
                    if(title == $(item).attr('_href')){
                        parent.$('#beforeHref').val($(item).attr('_href'));
                        parent.beforeHref();
                        return;
                    }
                });
            });
            function getJsMicrocircle(){
                $.ajax({
                    url:"/JsMicrocircle/getJsMicrocircle",
                    data:{"userId":user.data.userId},
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        //判断当前用户是否有状态为草稿的数据
                        if (data.data != null){
                            $("#state").val(1);
                            $("#artcleTitle").val(data.data.artcleTitle);
                            $("#artcleCategroyName").val(data.data.artcleCategroyName);
                            $("#acticleCategroyId").val(data.data.acticleCategroyId);
                            $("#microcircleId").val(data.data.microcircleId);
                            // $("#area").val(data.data.article);
                            layedit.setContent(area,data.data.article,false);
                        }
                    }
                })
            }

            draft = function draft(){
                var layForm = form.val("layForm");
                var areaContent = layedit.getContent(area);
               var  start = areaContent.indexOf("src")+5;
               var  end = areaContent.indexOf("alt")-2;
                areaContent= areaContent.substring(start,end);
                layForm = {
                    "acticleCategroyId": layForm.acticleCategroyId,//分类id
                    "artcleCategroyName": layForm.artcleCategroyName,//分类名称
                    "artcleTitle": layForm.artcleTitle,//文章标题
                    "details":"2",//文章状态
                    "createDate":getTime(),//文章创建时间
                    "article":layedit.getContent(area),//富文本编辑器中的内容
                    "artcleUrl":areaContent,//微圈图片路径
                    "userId":user.data.userId,//用户Id
                    "microcircleId":$("#microcircleId").val()//微圈Id
                }
                console.log(layForm);
                //如果值为2表示当前用户没有状态为草稿的数据进行添加操作
                if($("#state").val() == "2"){
                    $.ajax({
                        url:"/JsMicrocircle/addJsMicrocircle",
                        data:layForm,
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            if (data.data != null){
                                layer.msg("草稿保存成功");
                            } else{
                                layer.msg("草稿保存失败");
                            }
                        }
                    })
                }else{
                    //值为1进行修改操作
                    layForm = {
                        "acticleCategroyId": layForm.acticleCategroyId,//分类id
                        "artcleTitle": layForm.artcleTitle,//文章标题
                        "article":layedit.getContent(area),//富文本编辑器中的内容
                        "artcleUrl":areaContent,//微圈图片路径
                        "userId":user.data.userId,//用户Id
                        "microcircleId":$("#microcircleId").val(),//微圈Id
                        "details":"2",//文章状态
                        "releaseDate":"",//文章发布时间
                    }
                    $.ajax({
                        url:"/JsMicrocircle/updateJsMicrocircle",
                        data:layForm,
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            layer.msg("草稿修改成功");

                        }
                    })
                }

            }

            release = function release() {
                var layForm = form.val("layForm");
                var areaContent = layedit.getContent(area);
                var  start = areaContent.indexOf("src")+5;
                var  end = areaContent.indexOf("alt")-2;
                areaContent= areaContent.substring(start,end);
                layForm = {
                    "article":layedit.getContent(area),//富文本编辑器中的内容
                    "acticleCategroyId": layForm.acticleCategroyId,//分类id
                    "artcleCategroyName": layForm.artcleCategroyName,//分类名称
                    "artcleTitle": layForm.artcleTitle,//文章标题
                    "details":"5",//文章状态
                    "createDate":getTime(),//文章创建时间
                    "releaseDate":getTime(),//文章发布时间
                    "artcleUrl":areaContent,//微圈图片路径
                    "userId":user.data.userId,//用户Id
                    "microcircleId":$("#microcircleId").val()//微圈Id
                }
                console.log(layForm);
                if(layForm.acticleCategroyId != null && layForm.acticleCategroyId != ''&& layForm.artcleTitle != null && layForm.artcleTitle != ''){
                    if($("#state").val() == "2"){
                        $.ajax({
                            url:"/JsMicrocircle/addJsMicrocircle",
                            data:layForm,
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                var user = JSON.parse(localStorage.getItem("user"));
                                if (data.data != null){
                                    var param = {
                                        "courseId":data.data.microcircleId,
                                        "applyDate":getTime(),
                                        "launchId":user.data.userId,
                                        "result":2,
                                        "type":5
                                    };
                                    $.ajax({
                                        url:"/SyCourseRecord/addJsCourseRecord",
                                        data:param,
                                        type:'post',
                                        dataType:'json',
                                        success:function (data) {
                                            if (data.data != null){
                                                layer.msg("发布成功等待审核");
                                                location.reload();
                                                getJsMicrocircle();
                                                parent.$('#quickEntry').val("微圈列表");
                                                parent.quickEntry();
                                            }
                                        }
                                    })
                                } else{
                                    layer.msg("文章发布失败");
                                }
                            }
                        })


                    }else{
                        layForm = {
                            "acticleCategroyId": layForm.acticleCategroyId,//分类id
                            "artcleTitle": layForm.artcleTitle,//文章标题
                            "article":layedit.getContent(area),//富文本编辑器中的内容
                            "artcleUrl":areaContent,//微圈图片路径
                            "userId":user.data.userId,//用户Id
                            "microcircleId":$("#microcircleId").val(),//微圈Id
                            "details":"5",//文章状态
                            "releaseDate":getTime()//文章发布时间
                        }
                        $.ajax({
                            url:"/JsMicrocircle/updateJsMicrocircle",
                            data:layForm,
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                if (data.data != null){
                                    var param = {
                                        "courseId":data.data.microcircleId,
                                        "applyDate":getTime(),
                                        "launchId":user.data.userId,
                                        "result":2,
                                        "type":5
                                    };
                                    $.ajax({
                                        url:"/SyCourseRecord/addJsCourseRecord",
                                        data:param,
                                        type:'post',
                                        dataType:'json',
                                        success:function (data) {
                                            if (data.data != null){
                                                layer.msg("发布成功等待审核");
                                                getJsMicrocircle();
                                                parent.$('#quickEntry').val("微圈列表");
                                                parent.quickEntry();
                                            }
                                        }
                                    })

                                    getJsMicrocircle();
                                } else{
                                    layer.msg("草稿发布失败");
                                }
                            }
                        })
                    }
                }else{
                    if (layForm.artcleTitle == null || layForm.artcleTitle == ''){
                        layer.msg("当前文章没有标题");
                        return ;
                    }
                    if (layForm.acticleCategroyId == null || layForm.acticleCategroyId == ''){
                        layer.msg("请选择微圈分类");
                        return ;
                    }
                    if (layForm.article == null || layForm.article == ''){
                        layer.msg("当前文章没有内容");
                        return ;
                    }
                }


            }
            ArticleClassification = function ArticleClassification() {
                layer.open({
                    type:2,
                    title:"文章分类",
                    area:["500px","500px"],
                    offset:['150px','500px'],
                    content:"article-classification.html",
                });
            }
            //现在时间
            function getTime(){
                var date=new Date();
                var year=date.getFullYear();
                var month=date.getMonth()+1;
                var day=date.getDate();
                var hour=date.getHours();
                var minute=date.getMinutes();
                var second=date.getSeconds();
                var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
                return dateStr;
            }
            function zeroFilling(data){
                if(data<10){
                    data="0"+data;
                }
                return data;
            }
        })
    </script>
</html>